<?php
	$NoiDungSinhNhat = $_POST['NoiDungSinhNhat'];
	include "../include/class.phpmailer.php";
	include "../include/class.smtp.php";
	$loi=false;
	$noidungloi="";
	$dem =0;
	$khachhang = new khachhangbs();
	$danhsach = $khachhang->LayDanhSach();
	for($i = 0; $i < ($danhsach == false ? 0 : count($danhsach)); $i++)
	{
		$NgaySinh = $danhsach[$i]['NgaySinh'];
		$DateSinh = explode("-", $NgaySinh);
		$MonthSinh = $DateSinh[1];
		$DaySinh = $DateSinh[2];
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$NgayHienHanh = date("Y-m-d", time());
		$Date = explode("-", $NgayHienHanh);
		$MonthCurrent = $Date[1];
		$DayCurrent =  $Date[2];
		if($DayCurrent == $DaySinh && $MonthCurrent == $MonthSinh)
		{
			$mail  = new PHPMailer();
			$body = $NoiDungSinhNhat;
			$mail->IsSMTP();
			$mail->SMTPAuth  = true;                 #enable SMTP authentication
			$mail->SMTPSecure = "ssl";               #sets the prefix to the server
			$mail->CharSet="UTF-8";
			$mail->Host  = "smtp.gmail.com";         #sets GMAIL as the SMTP server
			$mail->Port       = 465;                 #set the SMTP port
			$mail->Username   = "heodatbietnoi1991";                  #your gmail username
			$mail->Password   = "thienthi90";                  #Your gmail password
			$mail->From       = "heodatbietnoi1991";                  #your gmail id
			$mail->FromName   = "Quỹ tín dụng Mỹ Hòa";                  #your name
			$mail->Subject    = "Chúc mừng sinh nhật";
			$mail->WordWrap   = 50;
			$mail->AddAddress($danhsach[$i]["Email"], $danhsach[$i]["Ho"]." ".$danhsach[$i]["Ten"]);
			$mail->IsHTML(true); // send as HTML
			$mail->Body = $body;
			if(!$mail->Send())
			{
				$loi=true;
		        $noidungloi = $mail->ErrorInfo;
			}
			$dem=$dem+1;
		}
	}
	if($dem<1)
	{
		$_SESSION['GuiSinhNhat'] =  "Không có khách hàng nào có ngày sinh nhật hôm nay";
	}
	elseif($loi==true)
	{
		$_SESSION['GuiSinhNhat'] =  "Lỗi khi gửi mail "+$noidungloi;
	}
	else
	{
		$_SESSION['GuiSinhNhat'] = "Đã gửi E-mail chúc mừng sinh nhật đến khách hàng";
		session_start();
		$nhatky = new nhatkybs();
		$nhatky->TenSuKien = "Gửi thư sinh nhật đến khách hàng";
		$nhatky->MaNguoiDung = $_SESSION["MaND"];
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
		$nhatky->Them();
	}
	header("Location:index.php?do=danhsachsinhnhatkhachhang");
?>